﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using TrainingManagement.Models;

namespace TrainingManagement.Controllers
{
    public class EmpTrainingAPIController : ApiController
    {
        private trainingDBEntities db = new trainingDBEntities();

        // GET api/EmpTrainingAPI
        public IEnumerable<tblEmpTraining> GettblEmpTrainings()
        {
            return db.tblEmpTrainings.AsEnumerable();
        }

        // GET api/EmpTrainingAPI/5
        public tblEmpTraining GettblEmpTraining(int id)
        {
            tblEmpTraining tblemptraining = db.tblEmpTrainings.Single(t => t.ID == id);
            if (tblemptraining == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return tblemptraining;
        }

        // PUT api/EmpTrainingAPI/5
        public HttpResponseMessage PuttblEmpTraining(int id, tblEmpTraining tblemptraining)
        {
            if (ModelState.IsValid && id == tblemptraining.ID)
            {
                db.tblEmpTrainings.Attach(tblemptraining);
                db.ObjectStateManager.ChangeObjectState(tblemptraining, EntityState.Modified);

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/EmpTrainingAPI
        public HttpResponseMessage PosttblEmpTraining(tblEmpTraining tblemptraining)
        {
            if (ModelState.IsValid)
            {
                db.tblEmpTrainings.AddObject(tblemptraining);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, tblemptraining);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = tblemptraining.ID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/EmpTrainingAPI/5
        public HttpResponseMessage DeletetblEmpTraining(int id)
        {
            tblEmpTraining tblemptraining = db.tblEmpTrainings.Single(t => t.ID == id);
            if (tblemptraining == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.tblEmpTrainings.DeleteObject(tblemptraining);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, tblemptraining);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}